package cn.wqk.rrtf.service;

import cn.wqk.rrtf.mapper.ManagerMapper;
import cn.wqk.rrtf.mapper.UserMapper;
import cn.wqk.rrtf.pojo.Manager;
import cn.wqk.rrtf.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
@Transactional
public class ManagerServiceImpl implements MangaerService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ManagerMapper managerMapper;
    @Override
    public int registerManager(User user, Manager manager) throws DataAccessException{
        try {
            userMapper.insertUser(user);
            Integer userId = user.getUserId();
            manager.setUserId(userId);
            managerMapper.insertManager(manager);
            return userId;
        }catch (DataAccessException e){
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            System.out.println(e);
            return -1;
        }
    }



    @Override
    public List<Manager> selectManager(Manager manager) {
        List<Manager> managerList=managerMapper.selectManagerByCondition(manager);
        return managerList;
    }

    @Override
    public int deleteManager(Integer memberId) {
        return managerMapper.deleteManagerByManagerId(memberId);
    }

    @Override
    public int updateManager(Manager manager) {
        return managerMapper.updateManagerByManagerId(manager);
    }
}
